package com.sceneworker;

import java.awt.Frame;
import java.lang.reflect.Constructor;
import java.util.Hashtable;

import javax.swing.tree.TreeModel;

import com.acarter.composabletree.ComposableTreeModel;
import com.acarter.jmejtree.JMEComposableTreeModel;
import com.acarter.jmejtree.JMEPhysicsComposableTreeModel;
import com.acarter.scenemonitor.dialog.MonitorDialog;
import com.acarter.scenemonitor.dialog.MonitorDialog.NodeModelWrapper;
import com.acarter.scenemonitor.util.Util;
import com.jme.input.KeyBindingManager;
import com.jme.renderer.pass.BasicPassManager;
import com.jme.renderer.pass.Pass;
import com.sceneworker.jmetreeextensions.SceneWorkerComposableTreeModel;
import com.sceneworker.jmetreeextensions.SceneWorkerPhysicsTreeModel;
import com.sceneworker.jmetreeextensions.keybindingmanager.KeyBindingManagerTreeModel;
import com.sceneworker.passmonitor.PassManagerTreeModel;

public class SceneWorkerAppMonitorDialog extends MonitorDialog {

	static	private		Hashtable<Class<?>, Class<?>>		mcla_objectTreeBindings = new Hashtable<Class<?>, Class<?>>();

	static	public	void	addTreeRepresentationBinding(Class<?> cl_objectClass, Class<? extends ComposableTreeModel> cl_treeRepClass) {
		mcla_objectTreeBindings.put(cl_objectClass, cl_treeRepClass);
	}
	
	public SceneWorkerAppMonitorDialog(Frame owner) {
		super(owner);
		
		initialiseDefualtTreeBindings();
	}
	
	private	void	initialiseDefualtTreeBindings() {
		addTreeRepresentationBinding(BasicPassManager.class, PassManagerTreeModel.class);
		addTreeRepresentationBinding(KeyBindingManager.class, KeyBindingManagerTreeModel.class);
	}
	
	@Override
	public void registerNode(Object node, String displayName) {
		NodeModelWrapper w = new NodeModelWrapper();
		w.displayName = displayName;
		
		Class<?>		lcl_treeRepClass = mcla_objectTreeBindings.get(node.getClass());
		
		if(lcl_treeRepClass != null) {
			try {
				Constructor<?>		lcl_ctor = lcl_treeRepClass.getConstructor(Object.class);
				w.treeModel = (ComposableTreeModel)lcl_ctor.newInstance(node);
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
			
		} else {
			// default is to treat the object just as SceneMonitor does 
			if(Util.detectPhysics()) {
				w.treeModel = new SceneWorkerPhysicsTreeModel(node);
			}	
			else {
				w.treeModel = new SceneWorkerComposableTreeModel(node);
			}
		}
		
		registeredNodes.put(node, w);

		refreshNodeSelection();
		
		//super.registerNode(node, displayName);
	}
}
